import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

// 路由懒加载
const Login = () => import('views/login/Login')
const Reg = () => import('views/reg/Reg')
const Main = () => import('views/main/Main')
const Home = () => import('views/menus/home/Home')
const UserInfo = () => import('views/menus/user/UserInfo')
const UserAvatar = () => import('views/menus/user/UserAvatar')
const UserPwd = () => import('views/menus/user/UserPwd')
const ArtCate = () => import('views/menus/article/ArtCate')
const ArtList = () => import('views/menus/article/ArtList')

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/reg',
    component: Reg
  },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: Home
      },
      {
        path: 'user-info',
        component: UserInfo
      },
      {
        path: 'user-avatar',
        component: UserAvatar
      },
      {
        path: 'user-pwd',
        component: UserPwd
      },
      {
        path: 'art-cate',
        component: ArtCate
      },
      {
        path: 'art-list',
        component: ArtList
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 前置路由守卫   权限验证
router.beforeEach((to, from, next) => {
  const arr = ['/login', '/reg']
  if (!store.state.token && !arr.includes(to.path)) {
    Vue.prototype.$message.error('请您先登录哦！')
    return next('/login')
  }
  next()
})

export default router
